package energyware.gui;

import java.util.ArrayList;
import java.util.List;

import energyware.interfaces.TestConnectionInterface;
import energyware.queries.Queries;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ConfigActivity extends Activity implements OnClickListener, TestConnectionInterface{

	private Spinner dropDowntimes;
	private Button accept;
	@SuppressWarnings("unused")
	private TextView txtgpstime;

	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		 setContentView(R.layout.config);
	     //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		 txtgpstime = (TextView) findViewById(R.id.txtgpstime);
		 
		 accept = (Button) findViewById(R.id.accept_gps_time);
		 
		 dropDowntimes = (Spinner) findViewById(R.id.droplistGPSTime);
		 
		 populateSpinner();
		 
		 Queries query = new Queries();
		 String gpstime = query.getGPSTIMEConfig(getApplicationContext());
		 int pos = 0;
		 if (gpstime.equals("2MIN")){
			 pos = 0;
		 }else if(gpstime.equals("5MIN")){
			 pos = 1;
		 }else if(gpstime.equals("10MIN")){
			 pos = 2;
		 }
		 
		 dropDowntimes.setSelection(pos);
		 
		 accept.setOnClickListener(this);
 		  
		
	}
	
	public boolean isOnline() {
		try{
		    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		    NetworkInfo netInfo = cm.getActiveNetworkInfo();
		    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
		        return true;
		    }
	    return false;
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	return false;
	    }
	}

	public void onClick(View v) {
		
		if(v.getId()== R.id.accept_gps_time){
		
			String getTime = dropDowntimes.getSelectedItem().toString();
			String[] time = getTime.split(" ");
			Queries q = new Queries();
			q.updateGPSTIMEConfig(getApplicationContext(), time[0]);
			
		}
		  
		 Toast.makeText(this, getString(R.string.Configuration_changed), Toast.LENGTH_SHORT).show();
		 Intent i = new Intent(getApplicationContext(), CoreMenuActivity.class);
	     startActivity(i);
	}
	
	
	
public void populateSpinner(){
           
        

        List<String> list = new ArrayList<String>();
        
            
            list.add(getString(R.string.two_minutes));
            list.add(getString(R.string.five_minutes));
            list.add(getString(R.string.ten_minutes));
        
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            dropDowntimes.setAdapter(dataAdapter);
           
        
    }

}
